<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/30
 * Time: 9:12
 */

namespace plugin\admin\app\logic\user;


use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\image\ImageCommandPlanModel;
use plugin\admin\app\model\image\ImageModelModel;
use plugin\admin\app\model\novel\NovelChapterModel;
use plugin\admin\app\model\novel\NovelChapterPartModel;
use plugin\admin\app\model\novel\NovelSpreadModel;
use plugin\admin\app\model\plan\BackgroundMusicModel;
use plugin\admin\app\model\tencent\TimbreModel;
use plugin\admin\app\model\user\UserPromotionArticleModel;
use plugin\admin\app\model\user\UserPromotionPlanModel;
use plugin\admin\app\model\user\UserPromotionVideoModel;
use support\Request;
use support\Response;

class VideoLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new UserPromotionVideoModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }

        //$request->inputSet('isdel','0');
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        $musicMap = $this->getListMapById(BackgroundMusicModel::getInstance(),$items,'musicid','id');
        $timbreMap = $this->getListMapById(\plugin\admin\app\model\moyin\TimbreModel::getInstance(),$items,'timbreid','id');
        $fontMap = $this->getListMapById(FontModel::getInstance(),$items,'fontid','id');
        $modelMap = $this->getListMapById(ImageCommandPlanModel::getInstance(),$items,'templateid','id');
        foreach($items as &$val){

            $val['musicname'] = isset($musicMap[$val['musicid']])?$musicMap[$val['musicid']]['name']:$val['musicid'];
            $val['music_url'] = isset($musicMap[$val['musicid']])?$musicMap[$val['musicid']]['music_url']:'';

            $val['fontname'] = isset($fontMap[$val['fontid']])?$fontMap[$val['fontid']]['name']:$val['fontid'];
            $val['modelname'] = isset($modelMap[$val['templateid']])?$modelMap[$val['templateid']]['name']:$val['templateid'];

            $val['timbrename'] = isset($timbreMap[$val['timbreid']])?
                $timbreMap[$val['timbreid']]['name'].'('.$timbreMap[$val['timbreid']]['scenes'].')':$val['timbreid'];

        }
        return parent::formatNormal($items, $total);
    }

    public function partsDetail(Request $request): Response
    {
         $detail = $this->model->find($request->input('id'));
         $contents = json_decode($detail['part_content'], true);
         $subtitles = json_decode($detail['subtitle'], true);

        $data = [];
        if($contents){
            foreach($contents as $k=>$val){
                $data[] = [
                    'id'=>$k+1,
                    'content'=>$val,
                    'subtitles'=>$subtitles[$k]??[],
                ];
            }
        }

        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

}